package com.ruibang.glass.material.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruibang.glass.material.constant.Constants;
import com.ruibang.glass.material.domain.req.MaterialKineSideWareHouseReq;
import com.ruibang.glass.material.dto.RqStatusStaticsDTO;
import com.ruibang.glass.material.dto.WareHouseAndLocation;
import com.ruibang.glass.material.dto.WareHouseInfoDTO;
import com.ruibang.glass.material.entity.MaterialLineSideWarehouseLocation;
import com.ruibang.glass.material.service.MaterialLineSideWarehouseLocationService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * <p>
 * 物管 全部库位 前端控制器
 * </p>
 *
 * @author mameng
 * @since 2024-02-05
 */
@RestController

@RequestMapping("/api/material/line/ware/house")
@Api(value = "物管库位", tags = {"物管库位"})
public class MaterialAllWareHouseController {


    @Autowired
    private MaterialLineSideWarehouseLocationService materialLineSideWarehouseLocationService;


    @PostMapping("/list")
    @ApiOperation(value = "库位查询", response = ResultBody.class)
    public ResultBody getPageList(@RequestBody MaterialKineSideWareHouseReq materialKineSideWareHouseReq) {
        return ResultBody.success(materialLineSideWarehouseLocationService.getPage(materialKineSideWareHouseReq));
    }

    @PostMapping("/getLocationList")
    @ApiOperation(value = "库位查询(不分页)", response = ResultBody.class)
    public ResultBody getLocationList(@RequestBody MaterialKineSideWareHouseReq materialKineSideWareHouseReq) {
        return ResultBody.success(materialLineSideWarehouseLocationService.getLocationList(materialKineSideWareHouseReq));
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新", response = ResultBody.class)
    public ResultBody updateMaterialLineSideWarehouseLocation(@RequestBody MaterialLineSideWarehouseLocation materialManagement) {
        return ResultBody.success(materialLineSideWarehouseLocationService.updateById(materialManagement));
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增", response = ResultBody.class)
    public ResultBody addMaterialLineSideWarehouseLocation(@RequestBody MaterialLineSideWarehouseLocation materialManagement) {
        return ResultBody.success(materialLineSideWarehouseLocationService.save(materialManagement));
    }

    @GetMapping("/getOne/{id}")
    @ApiOperation(value = "打印", response = ResultBody.class)
    public ResultBody getOneById(@PathVariable("id") String id) {
        return ResultBody.success(materialLineSideWarehouseLocationService.getById(id));
    }


    @PostMapping("/updateStatus")
    @ApiOperation(value = "更新通过状态", response = ResultBody.class)
    public ResultBody updateStatus(@RequestBody MaterialLineSideWarehouseLocation materialManagement) {
        materialLineSideWarehouseLocationService.updateMaterialLineSideWarehouseLocation(materialManagement);
        return ResultBody.success();
    }

    @PostMapping("/deleteChoose")
    @ApiOperation(value = "选择删除", response = ResultBody.class)
    public ResultBody deleteChoose(@RequestBody MaterialLineSideWarehouseLocation materialManagement) {
        materialLineSideWarehouseLocationService.removeBatchByIds(materialManagement.getIds());
        return ResultBody.success();
    }


    @GetMapping("/get/warehouse/all")
    @ApiOperation(value = "获取所有仓库名称", response = ResultBody.class)
    public ResultBody getAll() {
        List<MaterialLineSideWarehouseLocation> list = materialLineSideWarehouseLocationService
                .list(Wrappers.<MaterialLineSideWarehouseLocation>lambdaQuery().eq(MaterialLineSideWarehouseLocation::getStatus, Constants.ONE_STR));
        List<String> collect = list.stream().map(MaterialLineSideWarehouseLocation::getWarehouseName).distinct().collect(Collectors.toList());
        return ResultBody.success(collect);
    }

    @GetMapping("/get/locationById/{id}")
    @ApiOperation(value = "更具厂库获取库位Id", response = ResultBody.class)
    public ResultBody getAll(@PathVariable("id") String id) {
        List<MaterialLineSideWarehouseLocation> list = materialLineSideWarehouseLocationService
                .list(Wrappers.<MaterialLineSideWarehouseLocation>lambdaQuery().select(MaterialLineSideWarehouseLocation::getId,
                        MaterialLineSideWarehouseLocation::getLocationName).eq(MaterialLineSideWarehouseLocation::getWarehouseId, id).eq(MaterialLineSideWarehouseLocation::getStatus, Constants.ONE_STR));
        return ResultBody.success(list);
    }


    @GetMapping("/get/warehouse/getAllWareHouseAndLocation")
    @ApiOperation(value = "出库申请获取仓库和库位", response = ResultBody.class)
    public ResultBody getAllWareHouseAndLocation() {
        List<WareHouseAndLocation> allWareHouseAndLocation = materialLineSideWarehouseLocationService.
                getAllWareHouseAndLocation();
        return ResultBody.success(allWareHouseAndLocation);
    }

    @GetMapping("/get/warehouse/location/{name}")
    @ApiOperation(value = "位置", response = ResultBody.class)
    public ResultBody getLocation(@PathVariable("name") String name) {
        List<MaterialLineSideWarehouseLocation> list = materialLineSideWarehouseLocationService
                .list(Wrappers.<MaterialLineSideWarehouseLocation>lambdaQuery().select(MaterialLineSideWarehouseLocation::getLocationCode, MaterialLineSideWarehouseLocation::getLocationName).eq(MaterialLineSideWarehouseLocation::getStatus, Constants.ONE_STR)
                        .eq(MaterialLineSideWarehouseLocation::getWarehouseName, name)
                );
        Set<MaterialLineSideWarehouseLocation> uniqueSet = new LinkedHashSet<>(list);
        List<MaterialLineSideWarehouseLocation> uniqueList = new ArrayList<>(uniqueSet);
        return ResultBody.success(uniqueList);
    }


    @GetMapping("/get/warehouse/locationCode")
    @ApiOperation(value = "获取所有库位编码", response = ResultBody.class)
    public ResultBody getLocation() {
        List<WareHouseInfoDTO> allWareHouseInfoDTO = materialLineSideWarehouseLocationService.
                getAllWareHouseInfoDTO();
        return ResultBody.success(allWareHouseInfoDTO);
    }

    @GetMapping("/get/warehouse/itemList")
    @ApiOperation(value = "获取所有物品库位编码", response = ResultBody.class)
    public ResultBody<List<WareHouseInfoDTO>> getLocationItemList() {
        List<WareHouseInfoDTO> allWareHouseInfoDTO = materialLineSideWarehouseLocationService.
                getLocationItemList();
        return ResultBody.success(allWareHouseInfoDTO);
    }


    @GetMapping("/get/getCodeListById/{warehouseCode}")
    @ApiOperation(value = "根据模块获取不同模块库位下拉", response = ResultBody.class)
    public ResultBody getCodeListById(@PathVariable("warehouseCode") String warehouseCode) {
        List<MaterialLineSideWarehouseLocation> list = materialLineSideWarehouseLocationService.list
                (Wrappers.<MaterialLineSideWarehouseLocation>lambdaQuery().select(MaterialLineSideWarehouseLocation::getId, MaterialLineSideWarehouseLocation::getLocationName).eq(MaterialLineSideWarehouseLocation::getWarehouseCode, warehouseCode));
        return ResultBody.success(list);
    }


    @GetMapping("/get/rq/status/statics")
    @ApiOperation(value = "容器位置状态统计", response = ResultBody.class)
    public ResultBody getRqStatus() {
        return ResultBody.success(materialLineSideWarehouseLocationService.
                getRqStatus());
    }

    @PostMapping("/get/rq/status/list")
    @ApiOperation(value = "容器位置状态统计列表", response = ResultBody.class)
    public ResultBody updateStatusList(@RequestBody MaterialKineSideWareHouseReq materialManagement) {
        List<RqStatusStaticsDTO> rqStatusList = materialLineSideWarehouseLocationService.getRqStatusList(materialManagement);
        return ResultBody.success(rqStatusList);
    }


}
